package top.lshaci.learning.atguigu.interview3.aqs;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * LockSupportParkUnparkDemo
 *
 * @author lshaci
 * @since 1.0.0
 */
public class LockSupportParkUnparkDemo {

    public static void main(String[] args) throws InterruptedException {
        System.err.println("===== 先等待，再唤醒 =====");
        Thread a = new Thread(() -> {
            System.err.println(Thread.currentThread().getName() + " ==> IN");
            // 阻塞，等待唤醒
            LockSupport.park();
            System.err.println(Thread.currentThread().getName() + " ==> OUT");
        }, "A");

        a.start();
        new Thread(() -> {
            System.err.println(Thread.currentThread().getName() + " ==> 唤醒");
            LockSupport.unpark(a);
        }, "B").start();

        TimeUnit.SECONDS.sleep(5);

        System.err.println("===== 先唤醒，再等待 =====");
        Thread c = new Thread(() -> {
            System.err.println(Thread.currentThread().getName() + " ==> IN");
            // 相当于该行代码不生效
            LockSupport.park();
            System.err.println(Thread.currentThread().getName() + " ==> OUT");
        }, "C");

        new Thread(() -> {
            System.err.println(Thread.currentThread().getName() + " ==> 唤醒");
            LockSupport.unpark(c);
        }, "D").start();
        c.start();
    }
}
